/* eslint-disable @typescript-eslint/no-unused-vars */
// 运行时配置

import Cookies from 'js-cookie';
import { history } from 'umi';
import defaultSettings from '../config/defaultSettings';
import TopHeader from './components/Header';
import SideMenu from './components/SiderMenu';
import BasicLayout from './layouts/BasicLayout';

export const getCustomTabs = () => {
  return ({
    isKeep,
    keepElements,
    navigate,
    dropByCacheKey,
    local,
    activeKey,
  }: any) => {
    console.log({
      isKeep,
      keepElements,
      navigate,
      dropByCacheKey,
      local,
      activeKey,
    });
    return <></>;
  };
};

// 全局初始化数据配置，用于 Layout 用户信息和权限初始化
// 更多信息见文档：https://umijs.org/docs/api/runtime-config#getinitialstate
export async function getInitialState(): Promise<{
  currentUser?: Record<string, string>;
}> {
  // 判断用户是否登录
  if (!Cookies.get('token')) {
    history.push('/login');
    return {};
  }
  return {};
}

// 文档参见：https://ebee.paas.cmbchina.cn/pro-components/components/layout
export const layout: any = ({ initialState, setInitialState }: any) => {
  return {
    ...defaultSettings,
    menu: {
      locale: false,
    },
    headerRender: () => <TopHeader />,
    menuRender: (props: any) => <SideMenu {...props} />,
    waterMarkProps: {
      content: initialState?.currentUser?.name,
    },
    footerRender: false,
    onPageChange: () => {
      // 如果没有登录，重定向到登录页
      // if (!initialState?.currentUser && location.pathname !== loginPath) {
      //   history.push(loginPath);
      // }
    },

    // 更改浏览器页签上的title
    pageTitleRender: (props: any, defaultPageTitle: any, info: any) => {
      console.log({ props, defaultPageTitle, info });
      return 'umi4-template-123123';
    },

    // proLayout布局，右侧内容区域
    childrenRender(_dom: any, props: any) {
      console.log({ childrenRenderProps: props });
      return <BasicLayout {...props} />;
    },
  };
};
